package com.cms.bs.j2ee.listener;

import com.cms.bs.app.shiro.session.CachingShiroSessionDao;
import com.cms.bs.util.Log4jUtil;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by jiang on 2016-11-15.
 */
public class ShiroSessionListener implements SessionListener {

    @Autowired
    private CachingShiroSessionDao sessionDao;

    @Override
    public void onStart(Session session) {
        // 会话创建时触发
        Log4jUtil.info(this.getClass().getName(), "ShiroSessionListener session {" + session.getId() + "} 被创建");
    }

    @Override
    public void onStop(Session session) {
        sessionDao.delete(session);
        // 会话被停止时触发
        Log4jUtil.info(this.getClass().getName(), "ShiroSessionListener session {" + session.getId() + "} 被销毁");
    }

    @Override
    public void onExpiration(Session session) {
        sessionDao.delete(session);
        //会话过期时触发
        Log4jUtil.info(this.getClass().getName(), "ShiroSessionListener session {" + session.getId() + "} 过期");
    }
}
